package cn.tedu.blackhorse.controller;

import cn.tedu.blackhorse.pojo.dto.StudentAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.StudentUpdateDTO;
import cn.tedu.blackhorse.pojo.vo.StudentListItemVO;
import cn.tedu.blackhorse.service.IStudentService;
import cn.tedu.blackhorse.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理品牌相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/student")
@Api(tags = "学生信息表")
@Validated
public class StudentController {

    @Autowired
    private IStudentService studentService;

    public StudentController() {
        log.info("创建控制器：StudentController");
    }


    // http://localhost:9088/student/addNew
    @ApiOperation("增加学生信息")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParam(name = "id", value = "学生id", required = true, dataType = "long")
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(StudentAddNewDTO studentAddNewDTO) {
        log.debug("开始处理【增加学生信息】的请求");
        studentService.addNew(studentAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9088/student
    @ApiOperation("学生信息列表")
    @ApiOperationSupport(order = 200)
    @GetMapping("")
    public JsonResult<List<StudentListItemVO>> list() {
        log.debug("开始处理【增加学生信息】的请求");
        List<StudentListItemVO> list = studentService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9088/student/12/delete
    @ApiOperation("删除学生")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "学生id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除学生】的请求：id={}", id);
        studentService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9088/student/update
    @ApiOperation("修改学生信息")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "学生id", required = true, dataType = "long")
    @PostMapping("/update")
    public JsonResult<Void> update(Long id, StudentUpdateDTO studentUpdateDTO) {
        log.debug("开始处理【修改学生信息】的请求");
        studentService.updateById(id,studentUpdateDTO);
        return JsonResult.ok();
    }


}
